/*
 * @Descripttion:
 * @version:
 * @Author: cxguo
 * @Date: 2019-06-28 14:53:52
 * @LastEditors: cxguo
 * @LastEditTime: 2020-08-18 20:25:04
 */
import Vue from 'vue'
// 下载的插件
import Request from 'luch-request'
import store from '@/store'
import _ from 'lodash'

import {
	removeToken,
	getToken
} from '@/common/auth.js'

const addErrorLog = errorInfo => {}

function handleResponse(res) {
	if (res.config.url.includes('/oauth/token')) return
	const title = res.data.title || ""
	const message = res.data.message || ""
}

const handleErrorStatus = (error) => {
	const status = error.statusCode
	const data = error.data || {}
	let message = data.message

	if (status === 424) {
		handlerLogout()
		return error.response
	}
	
	if (status === 426) {
		uni.showToast({
			icon: 'error',
			title: error.data.msg
		})
		return error.response
	}

	if ([400, 403, 500].includes(status)) {
		const title = error.data.title ? error.data.title + ":" : error.data.title
		uni.showToast({
			icon: 'error',
			title: title + error.data.message
		})
		return
	}

	return error.response
}

// 处理token失效登出
const handlerLogout = _.debounce(function() {
	removeToken()
	uni.showModal({
		title: '登录失效',
		showCancel: false,
		editable: true,
		placeholderText: '请输入密码重新登录',
		success: function(res) {
			const password = res.content
			const username = store.getters.appData.userInfo.username
			if (res.confirm) {
				console.log("password:", username, password)
				uni.showLoading({
					title: "登录中"
				})
				store.dispatch('user/login', {
					username,
					password
				}).then(res => {}).finally(_ => {
					uni.hideLoading()
				})
			}
		}
	})
}, 400)

class HttpRequest {
	constructor(baseUrl) {
		const http = new Request()
		this.baseUrl = baseUrl
		this.queue = {}
		this.http = http
		this.interceptors(http)
	}
	setInsideConfig(instance, options = {}) {
		const defaultHeaders = {
			'Content-Type': 'application/json; charset=utf-8',
			// 'X-URL-PATH': location.pathname,
			'Access-Control-Allow-Origin': '*',
			'source': 'APP_SCAN',
			'Authorization': getToken()
		}
		const reqHeaders = options.headers
		instance.setConfig((config) => {
			/* config 为默认全局配置*/
			config.baseURL = this.baseUrl
			config.header = Object.assign({
				...defaultHeaders
			}, reqHeaders)
			config = Object.assign(config, options)
			return config
		})
	}
	interceptors(instance) {
		// 请求拦截
		instance.interceptors.request.use(config => {
			return config
		}, error => {
			return Promise.reject(error)
		})
		// 响应拦截
		instance.interceptors.response.use(res => {
			handleResponse(res)
			return res
		}, error => {
			// 节流500ms内只处理一次error
			Vue.prototype.$u.debounce(() => {
				handleErrorStatus(error)
			}, 500, true)
			return Promise.reject(error)
		})
	}
	request(options) {
		const http = this.http
		this.setInsideConfig(http, options)
		return http.request(options)
	}
}
export default HttpRequest